const process = require('child_process');

const cluster = require('cluster');
const http = require('http');
const numCPUs = require('os').cpus().length;

// console.log(cluster);
// console.log(http);
// console.log(numCPUs);

module.exports = (app) => {
  // app.messenger.on('xxx_action', data => {
  //   console.log(data);
  // });
  app.beforeStart(async () => {
    await app.model.sync({ force: false }); // false 为不覆盖 true会删除再创建

    // 获取swagger配置文件
    const apiConfig = app.config.api;
    /**
     * 遍历配置文件，并配置设置
     */
    for (const x in apiConfig) {
      const config = apiConfig[x];
      const docsDefinition = {
        info: { // API informations (required)
          title: config.title || x, // Title (required)
          version: config.version || '0.0.0', // Version (required)
          description: config.description || x, // Description (optional)
        },
        host: '0.0.0.0:7001', // Host (optional)
        basePath: '/', // Base path (optional)
        files: config.files || `lib/docs`,
      };
      process.exec(`apidoc -i ${docsDefinition.files} -o app/public/apidoc-template -t lib/apidoc/template/ --parse-parsers apicontenttype=lib/apidoc/api_content_type.js`, function (error, stdout, stderr) {
        if (error !== null) {
          console.log('docs-${x} exec error: ' + error);
        } else {
          console.log(`docs-${x} is ready http://127.0.0.1:7001/public/apidoc-template/index.html`);
          console.log('\033[2J');
        }
      });
    }
  });
};
